﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Enterprise.Management.Models;
using Enterprise.Management.Common;
using System.IO;
using Enterprise.Management.Business;
using Enterprise.Management.Caches;
using Enterprise.Management.FilterAttributes;

namespace Enterprise.Management.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult LogOn()
        {
            LoginBLL bll = new LoginBLL();
            bll.Load();
            if (bll.Logged)
            {
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                UserBLL userBll = new UserBLL();
                var user = userBll.GetUser(model.UserName);
                if (user != null && userBll.ValidateUser(model.Password, user.User_Pwd))
                {
                    Log.Info(string.Format("{0} login successfully.", model.UserName));

                    LoginBLL status = new LoginBLL(user.User_ID);
                    status.Cache();

                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "用户名或密码不正确");
                }
            }

            return View(model);
        }

        public ActionResult LogOff()
        {
            LoginCache.GetCacheProvider().Clear();
            return RedirectToAction("LogOn", "Account");
        }

        [Login]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [Login]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                UserBLL userBll = new UserBLL();
                var user = HtmlCache.GetUser();

                if (!userBll.ValidateUser(model.OldPassword, user.User_Pwd))
                {
                    ModelState.AddModelError("OldPassword", "密码不正确");
                }
                else
                {
                    if (userBll.UpdatePassword(user.User_ID, model.NewPassword))
                    {
                        return RedirectToAction("ChangePasswordSuccess");
                    }
                    else
                    {
                        ModelState.AddModelError("", "旧密码失败，请稍后再试！");
                    }
                }
            }

            return View(model);
        }

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }

        public ActionResult ForgetPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgetPassword(ForgetPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                UserBLL userbll = new UserBLL();
                if (userbll.GetUser(model.Email) == null)
                {
                    ModelState.AddModelError("Email", "电子邮箱不存在");
                }
                else
                {
                    string url = string.Concat(Request.Url.GetLeftPart(UriPartial.Authority), Url.Action("ResetPassword"));
                    if (userbll.SendResetMail(model.Email, url))
                    {
                        return RedirectToAction("ForgetPasswordSuccess");
                    }
                    else
                    {
                        ModelState.AddModelError("", "重置密码邮件发送失败，请稍后再试！");
                    }
                }
            }

            return View(model);
        }

        public ActionResult ForgetPasswordSuccess()
        {
            return View();
        }

        public ActionResult ResetPassword(string m)
        {
            bool result = false;

            UserBLL userbll = new UserBLL();
            int uid;
            if (userbll.VerifyForgetPasswordLink(m, out uid))
            {
                result = userbll.ResetPassword(uid);
            }

            return View(result);
        }
    }
}
